<?php
/**
 *
 * ThinkPHP自定义标签
 * @Author:大象 576272452@qq.com
 */
class TagLibYs extends TagLib{
    // 标签定义
    protected $tags   =  array(
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'arclist' => array('attr' => 'table,field,limit,order,where,sql', 'level' => 3),
        'pagelist' => array('attr' => 'table,field,limit,order,where,pagesize,sql', 'level' => 3),
        'pages' => array('attr' => 'table,field,limit,order,where,pagesize,sql', 'close' => 0),
        );
    
    /**
     *+----------------------------------------------------------
     * arclist标签解析 插入可视化编辑器
     * 格式： <test:arclist sql="select * from xinwen"><li>标题：<{$vo.title}></li></test:arclist>
     * 或者： <test:arclist table="xinwen" limit='0,2'><li>标题：<{$vo.title}></li></test:arclist>   
     *+----------------------------------------------------------
     * @access public
     *+----------------------------------------------------------
     * @param string $attr 标签属性
     *+---------------------------------------------------------- 
     * @param string $content innerHtml部分
     *+----------------------------------------------------------
     * @return string|void
     *+----------------------------------------------------------
     */
    public function _arclist($attr,$content) {
        $tag = $this->parseXmlAttr($attr, 'arclist');//获得标签的变量
        if ($tag['table']) {   //根据用户输入的值拼接查询条件
            $sql = "M('{$tag['table']}')->";
            $sql .= ($tag['field']) ? "field({$tag['field']})->" : '';
            $sql .= ($tag['order']) ? "order({$tag['order']})->" : '';
            $sql .= ($tag['where']) ? "where({$tag['where']})->" : '';
            $sql .= ($tag['limit']) ? "limit('{$tag['limit']}')->" : '';
            $sql .= "select()";
        } else {
            if (!$tag['sql'])
                return ''; //排除没有指定model名称，也没有指定sql语句的情况
            $sql .= "M()->query('{$tag['sql']}')";
        }
		$tag["_sql"] ? $_sql = 1 : $_sql = 0;
        //下面拼接输出语句
        $parsestr = '<?php $_result=' . $sql . ';';
        $parsestr .= 'foreach($_result as $key=>$vo){?>';
        $parsestr .= $content; //解析在arclist标签中的内容
        $parsestr .= '<?php }if('.$_sql.'){echo M()->_sql();}?>';
        return $parsestr;
    }
	
    
    /**
     *+----------------------------------------------------------
     * list分页标签解析 插入可视化编辑器
     * 格式： <test:arclist sql="select * from xinwen"><li>标题：<{$vo.title}></li></test:arclist>
     * 或者： <test:arclist table="xinwen" limit='0,2'><li>标题：<{$vo.title}></li></test:arclist>   
     *+----------------------------------------------------------
     * @access public
     *+----------------------------------------------------------
     * @param string $attr 标签属性
     *+---------------------------------------------------------- 
     * @param string $content innerHtml部分
     *+----------------------------------------------------------
     * @return string|void
     *+----------------------------------------------------------
     */
    public function _pagelist($attr,$content){
        $tag = $this->parseXmlAttr($attr, 'pagelist');//获得标签的变量
        $Form = M($tag['table']);
        import("@.ORG.Util.Page");       //导入分页类
        $count = $Form->where($map)->count();    //计算总数
        $p = new Page($count, $tag['pagesize']);
        $list = $Form->where($map)->limit($p->firstRow . ',' . $p->listRows)->order('id desc')->select();
        $this->pages = $p->show();
        $string = serialize($list);
        $str = htmlspecialchars($string,ENT_QUOTES);
        //下面拼接输出语句
        $parsestr = '<?php $list="' . $str . '";$_result = unserialize(htmlspecialchars_decode($list,ENT_QUOTES));';
        $parsestr .= 'foreach($_result as $key=>$vo){?>';
        $parsestr .= $content; //解析在arclist标签中的内容
        $parsestr .= '<?php }?>';
        return $parsestr;
    
    }
    
    /**
     *+----------------------------------------------------------
     * pages分页标签解析 插入可视化编辑器
     * 格式： <test:pages />
     *+----------------------------------------------------------
     * @access public
     *+----------------------------------------------------------
     * @param string $attr 标签属性
     *+---------------------------------------------------------- 
     * @return string
     *+----------------------------------------------------------
     */
    public function _pages($attr){
        $parsestr = $this->pages;
        return $parsestr;
    }
}
?>